jQuery(document).ready(function() {
	jQuery("#mobile_number").focus(); 
    jQuery('#mobile_number').bind("cut copy paste",function(e) {
     	 e.preventDefault();
  	});	
	
	jQuery('#verif_code').bind("cut copy paste",function(e) {
     	 e.preventDefault();
  	});
	
	/*Hide Div*/
	jQuery("#verificationCode, .errorBox2").hide();	
	
	var current_pos = getCookie("current_pos");
	if (current_pos > 1) {
		//console.log(current_pos + "TRUE");
		if(current_pos=="2") {
		jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="3") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="4") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
		
		  var mode = getCookie("mode");
		  var promo = getCookieR("promo");
		  var commitmentBox = getCookieR("commitmentBox");
		  var mobtel = getCookieR("mobtel");
		  
          if(mode!="edit"){      
			jQuery(".promoBox, .commitmentBox, .prepaidBox").addClass("active");
			jQuery('#pname, #pemail, #pdob, #padd').hide();
			jQuery('#promo').html(promo);
			jQuery('#commitmentBox').text(commitmentBox);
			jQuery('#mobtel').text(mobtel);
		  }
		}
		if(current_pos=="5") {
		jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
		
		  var mode = getCookie("mode");
		  var promo = getCookieR("promo");
		  var commitmentBox = getCookieR("commitmentBox");
		  var mobtel = getCookieR("mobtel");
		  var username = getCookieR("username");
		  var eadd = getCookieR("eadd");
		  var bday = getCookieR("bday");
		  var address = getCookieR("address");
		  
          if(mode!="edit"){ 
			jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox").addClass("active");
			jQuery(".pnone").hide();
			jQuery('#promo').html(promo);
			jQuery('#commitmentBox').text(commitmentBox);
			jQuery('#mobtel').text(mobtel);
			jQuery('#name').text(username);
			jQuery('#email').text(eadd);
			jQuery('#dob').text(bday);
			jQuery('#add').text(address);
			jQuery("#pname, #pemail, #pdob, #padd").show();
		  }
		}
	} else {
		//alert(current_pos + "FALSE");
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	} 
	
	
	//called when key is pressed in numberfield
	$("#mobile_number").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
		return false;
		}
	});	

	$("#verif_code").keypress(function (e) {
		if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
		return false;
		}
	});	
	
	jQuery('#mobile_number').keypress(function (e) {
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#submit_mobtel").click();
	  }
	});
	
	jQuery('#verif_code').keypress(function (e) {
	 var key = e.which;
	 if(key == 13)  // the enter key code
	  {
		jQuery("#validate_verif_code").click();
	  }
	});
	
	jQuery("#submit_mobtel").click(function(){
		mobtel=jQuery("#mobile_number").val();
		if(mobtel.length!=11) {
			jQuery("#submit_mobtel").text("Send code");
			jQuery(".error1").show();
			jQuery(".error2").hide();
			jQuery("#verificationCode").hide();
			jQuery("#mobile_number").addClass("error");
		} else {
			jQuery("#prepaidNumber .errorBox").hide();
			
			jQuery.getJSON(baseurl + "prepaid/verifynumber/" + mobtel,{ajax: 'true'}, function(j){
				if(j.status==0) {
					jQuery("#submit_mobtel").text("Resend code");			
					jQuery(".error1, .error2, .errorBox2").hide();
					jQuery("#verif_code").val("");
					jQuery("#verif_code").removeClass("error");
					jQuery("#verificationCode").show();
					jQuery("#mobile_number").removeClass("error");
					jQuery("#verif_code").focus(); 
				} else if (j.status==-2){
				    jQuery(".error1").hide();
					jQuery(".error2").show();
					jQuery("#mobile_number").addClass("error");
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number").focus();
				} else {
					jQuery(".error1").show();
					jQuery(".error2").hide();
					jQuery("#mobile_number").addClass("error");
					jQuery("#verificationCode").hide();
					jQuery("#mobile_number").focus();
				}		
			});
		}
	});
	
	jQuery("#validate_verif_code").click(function() {
		vcode=jQuery("#verif_code").val();
		if(vcode.length==6) {
			jQuery("#verificationCode .errorBox2").hide();
			
			jQuery.getJSON(baseurl + "prepaid/verifycode/" + vcode,{ajax: 'true'}, function(j){
				if(j.status==0) {
					window.location=j.redirect;
					jQuery("#verif_code").removeClass("error");
					document.cookie="current_pos=" + "4" + "; path=/";
				} else {
					jQuery("#verificationCode .errorBox2").show();
					jQuery("#verif_code").addClass("error");
				}	
			});
		} else {
			jQuery("#verificationCode .errorBox2").show();
			jQuery("#verif_code").addClass("error");
		}
	});

	/*. . . Add underline to a.linkColor. . . */
	$("#prepaidNumber p a.linkColor").attr("style","text-decoration:underline;");
	/*. . . Add underline to a.linkColor. . . */
	
});


   //Retrieve Cookie
   function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
   }
   
   //Retrieve Cookie
   function getCookieR(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
   }